package com.yt.system.controller;

import com.yt.system.domain.Account;
import com.yt.system.domain.WeekScore;
import com.yt.system.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author SYN
 */
@Api(tags = "个人账户管理")
@RestController
public class AccountController {
    @Autowired
    AccountService accountService;
    /**
     * 获取个人总排名
     * @param aid
     * @return
     */
    public int getRank(Integer aid){
        int number=1;
        //获取按积分降序排序的List
        List<Account> allAccount = accountService.getAllAccount();
        for(Account account:allAccount){
            if(!account.getAid().equals(aid)){
                number+=1;
            }else{
                return number;
            }
        }
        return number;
    }
    /**
     * 根据积分降序排序查询所有账户信息
     * @return
     */
    @ApiOperation(value = "根据积分降序排序查询所有账户信息-----总积分排行榜")
    @GetMapping("/getAllAccount")
    public List<Account> getAllAccount(){
        return accountService.getAllAccount();
    }

    /**
     * 根据用户ID查询账户信息
     * @param aid
     * @return
     */
    @ApiOperation(value = "根据用户ID查询账户信息----个人总积分")
    @GetMapping("/getAccountByAid/{aid}")
    public WeekScore getAccountByAid(@PathVariable("aid") Integer aid){
        Account account = accountService.selectByPrimaryKey(aid);
        WeekScore weekScore = new WeekScore();
        weekScore.setNumber(getRank(aid));
        weekScore.setAid(aid);
        weekScore.setAacount(account.getAacount());
        weekScore.setAintegral(account.getAintegral());
        return weekScore;
    }

    /**
     * 按照账户名模糊搜索
     * @param str
     * @return
     */
    @ApiOperation(value = "按照账户名模糊搜索")
    @GetMapping("/searchLikeAccount/{str}")
    public List<Account> searchLikeAccount(@PathVariable("str") String str){
        return accountService.searchLikeAccount(str);
    }

}
